//引入axios
import axios from "axios";
import qsString from "querystring";
//设置基础地址
let baseUrl = '/api'

//封装拦截器
//请求拦截
axios.interceptors.request.use((req) => req);

//响应拦截器
axios.interceptors.response.use((res) => res.data);

/* 
get
url
params
*/
export const get = (url, params = {}) => {
  return new Promise((resolve, reject) => {
    axios
      .get(baseUrl+url, {
        params,
      })
      .then((res) => resolve(res))
      .catch((err) => reject(err));
  });
};

/* 
post
url
params
isFile
*/
export const post = (url, params = {}, isFile = false) => {
  let data = {};
  if (isFile) {
    data = new FormData();
    for (let i in params) {
      data.append(i, params[i]);
    }
  } else {
    data = qsString.stringify(params);
  }
  return new Promise((resolve, reject) => {
    axios
      .post(baseUrl+url, data)
      .then((res) => resolve(res))
      .catch((err) => reject(err));
  });
};
